package com.white.androidstudy.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.white.androidstudy.widget.AbsView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by ytf on 2017/9/22 022.
 * Description: 用户向导里面的波纹view
 */

public class WaterRippleView extends AbsView
{

    private Paint mPaint1,mPaint2,mPaint3;

    public WaterRippleView(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    private void init()
    {
        mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint1.setStyle(Paint.Style.STROKE);
        mPaint1.setColor(Color.WHITE);
        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint2.setStyle(Paint.Style.STROKE);
        mPaint2.setColor(Color.WHITE);
        mPaint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint3.setStyle(Paint.Style.STROKE);
        mPaint3.setColor(Color.WHITE);

        circles = new ArrayList<>();
    }

    @Override
    public int getDefaultWrapWidth()
    {
        return 100;
    }

    @Override
    public int getDefaultWrapHeight()
    {
        return 100;
    }

    int width,height;
    int centerX, centerY;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        centerX = width / 2;
        centerY = height/2;
        mPaint1.setStrokeWidth(width/80);
        mPaint2.setStrokeWidth(width/80);
        mPaint3.setStrokeWidth(width/80);

        Circle c1 = new Circle(mPaint1,centerX, centerY);
        Circle c2 = new Circle(mPaint2,centerX, centerY);
        Circle c3 = new Circle(mPaint3,centerX, centerY);
        circles.add(c1);
        circles.add(c2);
        circles.add(c3);
        if(thread != null)
        {
            thread.kill();
        }

        thread = new ExpandThread(0.5f, 0, centerX, 2, 20, circles);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        for(Circle c : circles)
        {
            c.draw(canvas);
        }
    }

    public void start()
    {
        if(thread != null && !thread.isOnRunning())
        {
            thread.start();
        }
    }

    @Override
    protected void onDetachedFromWindow()
    {
        super.onDetachedFromWindow();
        if(thread != null)
        {
            thread.kill();
            circles.clear();
        }
    }

    private ExpandThread thread;

    class ExpandThread extends Thread
    {
        float min, max;
        float interval;
        int delayTime;
        List<Circle> circles;
        boolean needExit;
        boolean onRunning;
        float offsets;
        private int offsetDistance;
        private int size;

        public ExpandThread(float offset, float minRadius, float maxRadius,
                            float distanceInterval, int intervalTime, List<Circle> circles)
        {
            offsets = offset;
            min = minRadius;
            max = maxRadius;
            interval = distanceInterval;
            delayTime = intervalTime;
            this.circles = circles;
            offsetDistance = (int) (offset * (max - min));
            size = circles.size();
        }

        public boolean isOnRunning()
        {
            return onRunning;
        }

        public void kill()
        {
            needExit = true;
        }

        @Override
        public void run()
        {
            while(!needExit)
            {
                onRunning = true;
                postInvalidate();
                SystemClock.sleep(delayTime);
                if(circles.size() > 0)
                {
                    for (int i = 0; i < size; i++)
                    {
                        Circle c = circles.get(i);
                        int nextIndex = (i+1 == size ? 0: i+1);
                        Circle next = circles.get(nextIndex);
                        if(i == 0 && !circles.get(size-1).isUpdated)
                        {
                            c.update(min,max, interval);
                        }else
                        {
                            if(c.isUpdated)
                            {
                                c.update(min,max, interval);
                            }
                        }

                        if(c.radius >= offsetDistance)
                        {
                            if(!next.isUpdated)
                            {
                                next.update(min,max, interval);
                            }
                        }
                    }
                }
            }
            onRunning = false;
        }
    }

    private List<Circle> circles;

    class Circle
    {
        int centerX, centerY;
        float radius;
        Paint mPaint;
        int alpha;
        boolean isUpdated;
        private String name;
        public Circle(Paint p, int cx, int cy)
        {
            Random rd = new Random();
            this.mPaint = p;
            centerX = cx;
            centerY = cy;
            alpha = 255;
            name = String.valueOf(rd.nextInt(100));
        }

        public void setCenter(int x, int y)
        {
            this.centerX = x;
            this.centerY = y;
        }

        private void setAlpha(int a)
        {
            this.alpha = a;
            mPaint.setAlpha(this.alpha);
        }

        public void update(float minR, float maxR, float radiusInterval)
        {
            isUpdated = true;
            radius += radiusInterval;
            if(radius >= maxR)
            {
                radius = minR;
                isUpdated = false;
            }
            alpha = (int) (255 * (1 - radius / maxR));
            setAlpha(alpha);
        }

        @Override
        public String toString()
        {
            return name;
        }

        public void draw(Canvas c)
        {
            c.drawCircle(centerX, centerY, radius, this.mPaint);
        }
    }

    @Override
    public void onGlobalLayout()
    {

    }
}
